<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<jsp:include page="../comunes/header.jsp" />
<h2>Listado de Pedidos de &Oacute;rdenes de Trabajo</h2>
<button type="button" class="btn_agregar" title="Agregar" 
        onclick="window.location.href = '<c:url value="/pedidos?form"/>'">Crear un Pedido</button>
<table class="tbl_listado">
   <thead><tr><th>N&deg;</th><th>Solicitante</th><th>Fecha</th><th>Proveedor</th>
         <th>Total<br/>OTs</th><th>Total<br/>Facturas</th><th>Importe</th><th>Descuento</th>
         <th>Retenci&oacute;n IVA</th><th>Comentarios</th><th>Estatus</th><th>Opc.</th></tr></thead>
   <tbody>
      <c:forEach var="pedido" items="${pedidos}">
         <c:url var="editUrl" value="/pedidos/${pedido.llave.numero}?serie=${pedido.llave.serie}" />
         <c:url var="pdfUrl" value="/pedidos/pdf?sp=${pedido.llave.serie}&np=${pedido.llave.numero}" />
         <tr>
            <td><c:out value="${pedido.llave.numero}"/></td>
            <td>
               <c:forEach items="${pedido.solicitud.detalles}" var="detalle">
                  <c:if test="${detalle.evento.paso.tipoPaso=='I' && detalle.instancia == 1}">
                     ${detalle.usuario.nombre}
                  </c:if>
               </c:forEach>
            </td>
            <td><fmt:formatDate pattern="dd/MM/yyyy" value="${pedido.fecha}" /></td>
            <td>
               <c:choose>
                  <c:when test="${empty pedido.proveedor}">Depto. Mtto y gestion AF</c:when>
                  <c:otherwise>
                     <c:out value="${fn:substring(pedido.proveedor.razonSocial, 0, 25)} (${pedido.proveedor.numero})"/>
                  </c:otherwise>
               </c:choose>
            </td>
            <td><c:out value="${pedido.ordenes.size()}" /></td>
            <td><c:out value="${pedido.facturas.size()}" /></td>
            <td>
               <c:set var="sumatoria" value="${0}" />
               <c:forEach items="${pedido.facturas}" var="factura">
                  <c:set var="sumatoria" value="${sumatoria+factura.subtotal+factura.montoIva}" />
               </c:forEach>$<c:out value="${sumatoria}"/>
            </td>
            <td><c:out value="${pedido.descuento}"/></td>
            <td><c:out value="${pedido.retencionIva}"/></td>
            <td><c:out value="${pedido.comentarios}"/></td>
            <td>
               <c:choose>
                  <c:when test="${pedido.estatus=='AB'}">Abierto</c:when>
                  <c:when test="${pedido.estatus=='EP'}">Espera Persona</c:when>
                  <c:when test="${pedido.estatus=='EA'}">Espera Autorizacion</c:when>
                  <c:when test="${pedido.estatus=='EM'}">Espera Material</c:when>
                  <c:when test="${pedido.estatus=='EC'}">Espera Cotizacion</c:when>
                  <c:when test="${pedido.estatus=='RA'}">Reabierto</c:when>
                  <c:when test="${pedido.estatus=='FI'}">Finalizado</c:when>
                  <c:when test="${pedido.estatus=='AN'}">Anulado</c:when>
                  <c:when test="${pedido.estatus=='SR'}">Sin Finalizar</c:when>
                  <c:otherwise>Otro...</c:otherwise>
               </c:choose>
            </td>
            <td>
               <c:if test="${!(pedido.estatus=='FI' || pedido.estatus=='EA')}">
                  <button type="button" class="btn_editar" title="Editar" 
                          onclick="window.location.href = '${editUrl}&ac=e'">E</button>
               </c:if>
               <button type="button" class="btn_pdf" title="Ver PDF" 
                       onclick="window.open('${pdfUrl}')">PDF</button>
            </td>
         </tr>
      </c:forEach>
   </tbody>
   <tfoot><tr><td colspan="11"></td>
         <td><button type="reset" class="btn_regresar" title="Regresar">Regresar</button></td></tr>
   </tfoot>
</table><c:url var="regrUrl" value="/index" />
<script type='text/javascript'>
   $(document).ready(function() {
      $(".btn_regresar").click(function() {
         window.location.replace('${regrUrl}');
      });
   });
</script>
<jsp:include page="../comunes/footer.jsp" />